<template>
  <div>
  <el-table :data="dataList" border style="width: 100%;" v-loading="dataListLoading">
    <el-table-column align="center" type="index" header-align="center" min-width="150" label="序号"></el-table-column>
    <el-table-column
      align="center"
      prop="quesInfo"
      header-align="center"
      min-width="150"
      label="题干"
    ></el-table-column>
    <el-table-column
      align="center"
      prop="answ"
      header-align="center"
      min-width="150"
      label="选项"
    >
        <template slot-scope="scope">
          <div v-for="(item, index) in scope.row.optionsList" :key="index">
            <span v-if="item.optionsList!==''">{{item.key}}:{{item.option}}</span>
          </div>
        </template>
    </el-table-column>
    <el-table-column
      align="center"
      header-align="center"
      min-width="150"
      label="答案"
    >
      <template slot-scope="scope">
        <div v-if="scope.row.quesType != '4'">{{scope.row.answ}}</div>
        <div v-else>{{scope.row.answ | parseAnsw}}</div>
      </template>
    </el-table-column>
    <el-table-column
      align="center"
      prop="quesType"
      header-align="center"
      min-width="150"
      label="题型"
    >
      <template slot-scope="scope">
        <span v-if="scope.row.quesType === '1'">单选题</span>
        <span v-if="scope.row.quesType === '2'">多选题</span>
        <span v-if="scope.row.quesType === '3'">判断题</span>
        <span v-if="scope.row.quesType === '4'">填空题</span>
      </template>
    </el-table-column>
    <el-table-column
      align="center"
      prop="feedback"
      header-align="center"
      min-width="150"
      label="纠错意见"
    ></el-table-column>

    <el-table-column
      align="center"
      prop="realname"
      header-align="center"
      min-width="150"
      label="纠错人（姓名)"
    ></el-table-column>
    <el-table-column
      align="center"
      prop="identityCard"
      header-align="center"
      min-width="150"
      label="身份证号"
    ></el-table-column>
    <el-table-column header-align="center" align="center" width="150" label="操作">
      <template slot-scope="scope">
        <el-button type="text" @click="isEdit(scope.row)">编辑</el-button>
        <el-button type="text" @click="isDelete(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="dataForm.page"
      :page-sizes="[10, 20, 50]"
      :page-size="dataForm.limit"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
<!--    单选-->
    <add-or-update ref="addOrUpdate" @refreshDataList="getDataList" />
<!--    多选-->
    <add-ormult-update  ref="addOrmultUpdate" @refreshDataList="getDataList"/>
<!--    判断-->
    <add-orjudge-update ref="addOrjudgeUpdate" @refreshDataList="getDataList"/>
<!--    填空-->
    <add-orfill-update ref="addOrfillUpdate" @refreshDataList="getDataList"/>
  </div>
</template>

<script>
  import AddOrUpdate from './quessingerchoiceqst-add-or-update'
  import AddOrmultUpdate from './quesmultichoiceqst-add-or-update'
  import AddOrjudgeUpdate from './quesjudgeqst-add-or-update'
  import AddOrfillUpdate from './fill-blank-add-or-update'
  export default {
    name: 'correction',
    data() {
      return {
        dataList: [],
        totalCount: 0,
        dataListLoading: false,
        answList: [],
        dataForm: {
          page: 1,
          limit: 10
        }
      }
    },
    components: {
      AddOrUpdate,
      AddOrmultUpdate,
      AddOrjudgeUpdate,
      AddOrfillUpdate
    },
    mounted() {
      this.getDataList()
    },
    filters: {
      parseAnsw(answ) {
        let answList = JSON.parse(answ)
        let answStr = ''
        if (answList != null && answList.length > 0) {
          for (let i = 0; i < answList.length; i++) {
            for (let j = 0; j < answList[i].length; j++) {
              if (j < answList[i].length - 1) {
                answStr += answList[i][j] + ' || '
              } else {
                answStr += answList[i][j]
              }
            }
            if (i < answList.length - 1) {
              answStr += ' && '
            }
          }
        }
        return answStr
      }
    },
    methods: {
      getDataList(page) {
        if (page) this.dataForm.page = page
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/exam-admin/correction/getQuesCorrectionList'),
          method: 'post',
          data: this.dataForm
        }).then(data => {
          if (data) {
            this.dataList = data.data.list
            this.totalCount = data.data.totalCount
          } else {
            this.dataList = []
            this.totalCount = 0
          }
          this.dataListLoading = false
        })
      },
      isDelete(id) {
        this.$confirm(`确定要进行删除操作吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/exam-admin/correction/delete'),
            method: 'post',
            data: {
              id: id
            }
              // || this.dataListSelections.map(item => item.id).join(',')
          }).then(data => {
            if (data && data.code === 200) {
              this.$notify.success({
                title: '提示',
                message: '操作成功'
              })
              this.getDataList()
            } else {
              this.$notify.warning({
                title: '提示',
                message: data.msg
              })
            }
          })
        })
      },
      sizeChangeHandle(val) {
        this.dataForm.limit = val
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.dataForm.page = val
        this.getDataList()
      },
      isEdit(row) {
        // 单选
        if (row.quesType === '1') {
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(row.quesId)
          })
        }
        // 多选
        if (row.quesType === '2') {
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrmultUpdate.init(row.quesId)
          })
        }
        // 判断
        if (row.quesType === '3') {
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrjudgeUpdate.init(row.quesId)
          })
        }
        if (row.quesType === '4') {
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrfillUpdate.open(row.quesId)
          })
        }
      }
    }
  }
</script>

<style scoped>

</style>
